package com.qf.service.impl;

import com.qf.dao.UserDao;
import com.qf.dao.impl.UserDaoImpl;
import com.qf.page.PageBean;
import com.qf.pojo.User;
import com.qf.service.UserService;

import java.util.List;

/**
 * @program: myproject
 * @description:
 * @author: mrsheng
 * @create: 2022-09-26 17:31
 **/


public class UserServiceImpl implements UserService {
    UserDao userDao=new UserDaoImpl();


//查询所有


    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }


    @Override
    public User login(String username, String password) {
        return userDao.login(username,password);
    }

    //添加
    @Override
    public int addUser(User user) {
        return userDao.addUser(user);
    }
    //通过姓名查询

    @Override
    public List<User> findByNmame(String name) {
        return userDao.findByNmame(name);
    }

    //修改

    @Override
    public int update(User user) {
        return userDao.update(user);
    }


    //根据id查询

    @Override
    public User findUserById(int i) {
        return userDao.findUserById(i);
    }


    //根据id删除
    @Override
    public int deleteUser(int id) {
        return userDao.deleteUser(id);
    }

    /**
     * @Description: pageBean
     * @Param: data:当前页数据
     * @return:
     * @Author: shengchunquan
     * @Date: 2022/9/28 11:10
     */


    //实现分页业务的逻辑
    @Override
    public PageBean<User> findAll(PageBean<User> pageBean) {
        int currentPage = pageBean.getCurrentPage();
        int pageSize = pageBean.getPageSize();
    //分页查询
        //当前页
        List<User> all = userDao.findAll((currentPage - 1) * pageSize, pageSize);
        pageBean.setData(all);

        //总记录数
        int count=userDao.count();
        pageBean.setCount(count);


        //总页数
        int totalPage=0;
        //如果总记录数取模每页显示条数，有余数那么总页数就+1页
        if ( count%pageSize==0){
            totalPage=count/pageSize;
        }else {
            totalPage=count/pageSize+1;
        }
        pageBean.setTotalPage(totalPage);
        return pageBean;
    }


    @Override
    public PageBean<User> findAll(PageBean<User> pageBean, String username) {
        int currentPage = pageBean.getCurrentPage();
        int pageSize = pageBean.getPageSize();
        //分页查询
        //当前页
        List<User> all = userDao.findAll((currentPage - 1) * pageSize, pageSize,username);
        pageBean.setData(all);

        //总记录数
        int count=userDao.count(username);
        pageBean.setCount(count);


        //总页数
        int totalPage=0;
        //如果总记录数取模每页显示条数，有余数那么总页数就+1页
        if ( count%pageSize==0){
            totalPage=count/pageSize;
        }else {
            totalPage=count/pageSize+1;
        }
        pageBean.setTotalPage(totalPage);
        return pageBean;
    }
}
